﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DrawControl.UI.View
{
    public partial class ViewControl : UserControl
    {
        public ViewControl()
        {
            InitializeComponent();
            this.Click += ToolControl_Click;
            this.MouseMove += ViewFormControl_MouseMove;
            this.MouseLeave += ViewFormControl_MouseLeave;
            this.MouseDown += ViewControl_MouseDown;
            foreach (Control item in this.Controls)
            {
                item.Click += ToolControl_Click;
                item.MouseMove += ViewFormControl_MouseMove;
                item.MouseLeave += ViewFormControl_MouseLeave;
                item.MouseDown += ViewControl_MouseDown;
            }
        }
        public event Action<object, MouseEventArgs> MouseDownCallback;
        private void ViewControl_MouseDown(object? sender, MouseEventArgs e)
        {
            var control = sender as Control;
            if (control is ViewControl vc)
            {
                MouseDownCallback?.Invoke(sender, e);
            }
            else
            {
                Control parent = control.Parent;
                while (!(parent is ViewControl))
                    parent = parent.Parent;
                var vfc = parent as ViewControl;
                MouseDownCallback?.Invoke(vfc, e);
            }
        }

        private void ViewFormControl_MouseLeave(object? sender, EventArgs e)
        {
            var control = sender as Control;
            if (control is ViewControl vc)
            {
                if (vc.Select) return;
            }
            else
            {
                Control parent = control.Parent;
                while (!(parent is ViewControl))
                    parent = parent.Parent;
                var vfc = parent as ViewControl;
                if (vfc.Select) return;
            }

            this.BackColor = NoSelectColor;
        }

        private void ViewFormControl_MouseMove(object? sender, MouseEventArgs e)
        {
            var control = sender as Control;
            if (control is ViewControl vc)
            {
                if (vc.Select) return;
            }
            else
            {
                Control parent = control.Parent;
                while (!(parent is ViewControl))
                    parent = parent.Parent;
                var vfc = parent as ViewControl;
                if (vfc.Select) return;
            }

            this.BackColor = HoverColor;
        }

        private void ToolControl_Click(object sender, EventArgs e)
        {
            
        }

        private void Form_FormClosing(object? sender, FormClosingEventArgs e)
        {
            
        }
        public Image Image
        {
            get => pictureBox1.Image;
            set
            {
                pictureBox1.Image = value;
            }
        }
        public Color SelectColor { get; set; } = Color.FromArgb(133, 97, 161);
        public Color NoSelectColor { get; set; } = Color.Transparent;
        public Color HoverColor { get; set; } = Color.FromArgb(217, 236, 255);
        private bool _select = false;
        public bool Select
        {
            get => _select;
            set
            {
                if (_select == value) return;

                _select = value;
                if (_select)
                    this.BackColor = SelectColor;
                else
                    this.BackColor = NoSelectColor;
            }
        }
        public SealType Seal { get; set; } = SealType.RectangleSeal;
    }
    public enum SealType
    {
        RectangleSeal,
        RingSeal
    }
}
